package com.digital.smartgrow.service.expert.random;

import com.digital.smartgrow.entity.expert.Expert;
import com.digital.smartgrow.service.expert.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by SunYuechao on 2022/7/16 下午8:25
 * 生成随机专家数，带权重
 */
@Component
public class RandomExpertGenerate {
     @Autowired
     private ExpertService expertService;

     /**
      * 根据权重选取一个
      * @param expertList
      * @return
      */
     public Expert generateOne(List<Expert> expertList){
          List<RandomExpert> randomExperts=new ArrayList<RandomExpert>();
          Random random=new Random();
           for (Expert e:expertList){
                randomExperts.add(new RandomExpert(e));
           }
          RandomExpert randomExpert = RandomExpertUtil.randomOne(randomExperts, random);
          return expertService.getById(randomExpert.getId());
     }

     /**
      * 根据权重选取多个
      * @param expertList
      * @param size
      * @return
      */
     public List<Expert> generateAll(List<Expert> expertList,Integer size) throws Exception {
          if(expertList.size()<size){
               throw new Exception("抽选专家数量大于现有专家数量");
          }
          List<RandomExpert> randomExperts=new ArrayList<RandomExpert>();
          Random random=new Random();
          for (Expert e:expertList){
               randomExperts.add(new RandomExpert(e));
          }
          List<RandomExpert> randomExpertsRes=new ArrayList<RandomExpert>();
          for(int i=0;i<size;i++){
               RandomExpert randomExpert = RandomExpertUtil.randomOne(randomExperts, random);
               randomExpertsRes.add(randomExpert);
               randomExperts.remove(randomExpert);
          }
          List<Expert> res=new ArrayList<>();

          for(RandomExpert randomExpert:randomExpertsRes){
               System.out.println("randomExpert = " + randomExpert.getId());

               Expert expert = expertService.getById(randomExpert.getId());
               res.add(expert);
          }
          return res;
     }

}
